﻿using System;

namespace Msngr
{
    class HashTable<K, T> where  T : IComparable
    {
        private LinkedList<Node>[] Table;
        private int length;

        public HashTable()
        {
            Table = new LinkedList<Node>[0];
        }

        public class Node : IComparable
        {
            public int key;
            public T data;

            public Node(int key, T data)
            {
                this.key = key;
                this.data = data;
            }

            //Implement
            public int CompareTo(object obj)
            {
                return 1;
            }
        }

        //Optimize
        private int Hash(String key)
        {
            int hash = 0;
            char[] chArr = key.ToCharArray();
            int n = chArr.Length;
            for (int i = 0; i <= n - 1; i++)
            {
                hash += (int)chArr[i] * (31 * n - i);
            }
            //Console.WriteLine(hash);
            return Math.Min(hash, int.MaxValue / 6);
        }

        public void Add(K key, T data)
        {
            int hash = Hash(key.ToString());
            if (Table.Length < hash) Array.Resize(ref Table, hash);
            if (Table[hash] == null) Table[hash] = new LinkedList<Node>();
            Table[hash].AddAtBeggining(new Node(hash, data));
            length++;
        }

        public void Remove(String key)
        {
            Remove(Hash(key), key);
        }

        //Fix
        private bool Remove(int hash, string key)
        {
            //if (Table[hash] != null)
            //{
            //    Table[hash].Remove(hash);
            //    Table[hash] = null;
            //    return true;
            //}
            //return false;
            return false;
        }

        public void Clear()
        {
            Table = new LinkedList<Node>[0];
        }

        //public override String ToString()
        //{
        //    String tableString = String.Empty;
        //    for (int i = 0; i <= Table.Length - 1; i++)
        //    {
        //        if (Table[i] != null) tableString += "Hash:" + i + " Data:" + Table[i].ToString() + "\n";
        //    }
        //    return tableString;
        //}
    }
}
